package org.csu.mypetstore.web.servlets;

import org.apache.commons.beanutils.BeanUtils;
import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/doregist")
public class DoRegistServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        Map<String, String[]> map = req.getParameterMap();
        String verifycode = req.getParameter("verifycode");
        //校验验证码
        HttpSession session = req.getSession();
        String checkcood_server = (String) session.getAttribute("CHECKCODE_SERVER");
        //保证验证码是一次性的
        session.removeAttribute("CHECKCODE_SERVER");
        if(!checkcood_server.equalsIgnoreCase(verifycode)){
            //验证码不正确
            req.setAttribute("regist_msg","验证码错误！请重新输入！");
            req.getSession().setAttribute("condition",map);
            req.getRequestDispatcher("/WEB-INF/jsp/account/NewAccountForm.jsp").forward(req,resp);

            return;
        }


        if (!req.getParameter("repeatedPassword").equals(req.getParameter("password"))){//判断两次密码是否一致
            System.out.println("密码两次输入不一致，请重新输入");
            req.setAttribute("regist_msg","密码两次输入不一致，请重新输入");
            req.getSession().setAttribute("condition",map);
            req.getRequestDispatcher("/WEB-INF/jsp/account/NewAccountForm.jsp").forward(req,resp);
            return;
        }
        AccountService service = new AccountService();
        try {
            if (service.is_exit(req.getParameter("username"))){
                //判断用户名是否已存在
                System.out.println("用户名已存在，请重新输入");
                req.setAttribute("regist_msg","用户名已存在，请重新输入");
                req.getSession().setAttribute("condition",map);
                req.getRequestDispatcher("/WEB-INF/jsp/account/NewAccountForm.jsp").forward(req,resp);
                return;
            }
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Account account = new Account();
        try {
            BeanUtils.populate(account,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        service.insertAccount(account);
        req.getSession().setAttribute("regist_ok","注册成功");
        resp.sendRedirect(req.getContextPath()+"/login");

    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
